package com.feather.common.data.simple;

import java.util.ArrayList;

import com.feather.common.data.Column;
import com.feather.common.data.ColumnCollection;

/**
 * @author flogyin
 */
public class SimpleColumnCollection implements ColumnCollection, java.io.Serializable {
	private static final long	serialVersionUID	= 1L;

	protected ArrayList<Column>	collection			= new ArrayList<Column>();

	@Override
	public int getCount() {
		return this.collection.size();
	}

	@Override
	public Column get(int columnIndex) {
		return this.collection.get(columnIndex);
	}

	@Override
	public Column get(String columnName) {
		int columnIndex = this.indexOf(columnName);
		return columnIndex > -1 ? this.collection.get(columnIndex) : null;
	}

	@Override
	public Column add() {
		SimpleColumn column = new SimpleColumn();
		this.collection.add(column);
		return column;
	}

	@Override
	public Column add(String columnName) {
		SimpleColumn column = new SimpleColumn();
		column.setName(columnName);
		this.collection.add(column);
		return column;
	}

	@Override
	public void add(Column column) {
		if (column != null)
			this.collection.add(column);
	}

	@Override
	public boolean contains(String columnName) {
		return indexOf(columnName) > -1 ? true : false;
	}

	@Override
	public int indexOf(Column column) {
		return column != null ? indexOf(column.getName()) : -1;
	}

	@Override
	public int indexOf(String columnName) {
		if (columnName != null) {
			int count = getCount();
			for (int i = 0; i < count; i++) {
				String temp = this.collection.get(i).getName();
				if (columnName.equalsIgnoreCase(temp))
					return i;
			}
		}
		return -1;
	}

	@Override
	public void remove(Column column) {
		int columnIndex = indexOf(column);
		if (columnIndex > -1)
			this.collection.remove(columnIndex);
	}

	@Override
	public void remove(String columnName) {
		int columnIndex = indexOf(columnName);
		if (columnIndex > -1)
			this.collection.remove(columnIndex);
	}

	@Override
	public void removeAt(int columnIndex) {
		this.collection.remove(columnIndex);
	}

	@Override
	public void clear() {
		this.collection.clear();
	}
}
